/*
 * Copyright 2010 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.splink.deepsplink.boot {
	import org.splink.deepsplink.config.ConfigData;
	import org.splink.deepsplink.config.PageData;
	import org.splink.deepsplink.deeplink.SwfAddressDeeplinkStrategy;
	import org.splink.library.tree.INode;
	import org.splink.library.tree.Node;
	import org.splink.library.tree.Tree;

	/**
	 * Bootstraps the the deepsplink framework, with the given configuration.
	 * 
	 * @param config the framework configuration
	 * @param strategy the </code>IBootStrategy</code> implementation whose <code>boot</code>
	 * method is invoked with a freshly constructed and prepared <code>IDeepsplinkFacade</code>
	 * 
	 * @see org.splink.deepsplink.boot.IDeepsplinkBootStrategy	 * @see org.splink.deepsplink.boot.IDeepsplinkFacade
	 * @author Max Kugland
	 */
	final public class Bootstrapper {

		/**
		 * @param config the configuration
		 * @param strategy the boot strategy
		 */
		public function Bootstrapper(config : ConfigData, strategy : IBootStrategy) {
			var tree : Tree = new Tree(new Node(config.pageData.id, config.pageData.clone()));
			toTree(tree.root, config.pageData);
			var facade : DeepsplinkFacade = new DeepsplinkFacade(tree, config, new SwfAddressDeeplinkStrategy(config));
			strategy.boot(facade);
			facade.start();
		}

		private static function toTree(parent : INode, data : PageData) : void {
			for each (var pd : PageData in data.pageDataArray) {
				var node : INode = new Node(pd.id, pd.clone());
				parent.add(node);
				toTree(node, pd);
			}
		}
	}
}
